package com.iheima.reggie_take.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iheima.reggie_take.common.CustomException;
import com.iheima.reggie_take.entity.Category;
import com.iheima.reggie_take.entity.Dish;
import com.iheima.reggie_take.entity.Setmeal;
import com.iheima.reggie_take.service.CategoryService;
import com.iheima.reggie_take.mapper.CategoryMapper;
import com.iheima.reggie_take.service.DishService;
import com.iheima.reggie_take.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author fzykd
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2023-01-17 14:46:07
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    //菜品
    @Autowired
    private DishService dishService;

    //套餐
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param ids
     */
    @Override
    public void remove(Long ids) {
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件 根据分类id进行查询
        dishQueryWrapper.eq(Dish::getCategoryId,ids);
        int count1 = dishService.count(dishQueryWrapper);
        //查询年前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        if (count1>0){
            //已经关联
            throw new CustomException("当前分类下关联了菜品,不能删除!");
        }

        //查询当前分类是否关联了套餐，如果已经关联，则抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int count2 = setmealService.count();
        if (count2>0) {
            //已经关联套餐 抛出一个业务异常
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }
        //正常删除分类
        super.removeById(ids);
    }
}




